/**
 * Created by whw-apple on 2017/9/22.
 */
//声明系统提示弹窗
function alert_tip(title,cont){
    var alert_html = '<div class="alert show">' +
        '<div class="alert_head"><i class="fa fa-exclamation-circle"></i> 系统提示：' + title + '</div>' +
        '<div class="alert_content">'  + cont + '</div>' +
        '<div class="alert_foot">' +
        '<button type="button" class="btn btn_prime">确定</button>' +
        '<button type="button" class="btn btn_default">取消</button>' +
        '</div>' +
        '</div>';
    $("body").find(".mask").show().after(alert_html).find(".alert:not(:last-child)").remove();
    $(".btn_prime,.btn_default").click(function(){
        $(".mask").hide();
        $(".alert").remove();
        $(".popup").hide();
    });
}
function popup_show(obj,name,alertOpen,alertCont,cName){
    $("body").addClass("no_scroll");
    var c_html = $.trim($("#" + obj).html());
    $(".body").find("popup");
    $(".popup_content").html(c_html);
    $(".popup_head").html(name);
    $(".popup").css('display','block').addClass(cName);
    $(".mask").css('display','block');
    tabs(".tabs_main");
    $(".popup_close,.btn_default").click(function(){
        $(".mask").hide();
        $(".popup").css('display','none').removeClass(cName);
        $("body").removeClass("no_scroll");
    });
    $(".btn_prime").click(function(){
        $(".mask").hide();
        $(".popup").css('display','none').removeClass(cName);
        $("body").removeClass("no_scroll");
        if (alertOpen == "yes"){
            alert_tip("",alertCont);
        }
    });
}
function tabs(tabarea){
    $(tabarea + " .tab_list .tab").each(function(){
        $(this).click(function(){
            $(this).addClass("active").siblings().removeClass("active");
            var c_name = $(this).attr("id");
            $(tabarea + " .tab_div." + c_name).show().siblings(".tab_div").hide();
        });
    });
}

$(function () {
    //判断页面是否登录，登录则直接到index.html，未登录则到login.html
    //var islogin = window.localStorage.getItem("isLogin");

    //if (islogin == null || islogin != "yes"){
    //    window.location.href = "login.html";
    //}

    //Auth: Ferguson<Ferguson.Mr.F@gmail.com>
    String.prototype.getNav = function (indexOf) {
        var navs = window.sessionStorage.getItem('nav');
        navs = navs ? JSON.parse(navs) : new Object();
        if (indexOf === true) {
            return navs.hasOwnProperty(this.toString())
        } else {
            return navs;
        }
    }

    String.prototype.setNav = function (name) {
        var navs = window.sessionStorage.getItem('nav');
        console.log(navs);
        navs = navs ? JSON.parse(navs) : new Object();
        navs[this.toString()] = name;
        window.sessionStorage.setItem('nav', JSON.stringify(navs));
    }

    String.prototype.removeNav = function () {
        var navs = window.sessionStorage.getItem('nav');
        navs = navs ? JSON.parse(navs) : new Object();
        if (navs.hasOwnProperty(this.toString())) {
            delete navs[this.toString()];
            console.log(navs);
             //iframe修改
            if (JSON.stringify(navs) == "{}") {
                var a = $('<iframe class="aHomeaIndex" src="/Home/Index" ></iframe>')
                $("#index_frame").append(a);
                a.siblings().remove();
                sessionStorage.setItem('nav', '');
                sessionStorage.setItem('nav-active', '');
            }
            window.sessionStorage.setItem('nav', JSON.stringify(navs));
            
        }
    }
    //========================================
    var strcookie = document.cookie;//获取cookie字符串
    var arrcookie = strcookie.split("; ");//分割
    var manCookie = ".ALMFORMMain";//主  Cookie
    var manCookieValues = "";
    var manCookieF = ".ALMFORMF";//副  Cookie
    var manCookieFValues = "";
    //$.ajax({
    //    type: "post",
    //    url: "/WS/WSCommon.asmx/GetWebFormCookieName",//这里通过设置url属性来获取xml
    //    contentType: "application/json;charset=utf-8",
    //    dataType: "json",
    //    async: false,
    //    //timeout: 1000,      //设定超时
    //    cache: false,       //禁用缓存
    //    success: function (data) {
    //        var formCookieName = $.parseJSON(data.d).cookieName;
    //        manCookie = formCookieName + "Main";
    //        manCookieF = formCookieName + "F";
    //    },
    //    error: function (x, e) {
    //        itemTextArray.length = 0;
    //        if (x.status != "404")
    //            alert(x.responseText);
    //    }
    //});
    //遍历匹配 主
    for (var i = 0; i < arrcookie.length; i++) {
        var arr = arrcookie[i].split("=");
        if (arr[0] == manCookie) {
            manCookieValues = arr[1];
        }
    }
    //遍历匹配 主
    for (var i = 0; i < arrcookie.length; i++) {
        var arr = arrcookie[i].split("=");
        if (arr[0] == manCookieF) {
            manCookieFValues = arr[1];
        }
    }
    if (manCookieValues!="") {
        if (manCookieValues != manCookieFValues) {
            sessionStorage.setItem('nav', '');
            sessionStorage.setItem('nav-active', '');
        }
    }
    
    //========================================

    if (nav = (new String).getNav()) {
        console.log(nav);
        for (var link in nav) {
            var tab = $('<li data-id="' + link + '">' + nav[link] + '<i class="fa fa-times"></i></li>');
            $('.tabs ul').append(tab);
            //iframe修改
            var hit = $(window).height() - 120;
            var reg = new RegExp("/", "g");
            var cla = link.replace(reg, 'a');
            var a = $('<iframe class="' + cla + '" src="' + link + '" style="display:none"></iframe>')
            $("#index_frame").append(a)
            a.height(hit)
        }
        var active = window.sessionStorage.getItem('nav-active');
        var i = $('.tabs ul').find('[data-id="' + active + '"]');
        i.addClass('active').siblings().removeClass('active');
        //iframe修改
        
        if (active == "" || active == null || active == undefined) {
            var hit = $(window).height() - 120;
            var a = $('<iframe class="aHomeaIndex" src="/Home/Index" ></iframe>')
            $("#index_frame").append(a)
            a.height(hit)
        } else {
            var reg1 = new RegExp("/", "g");
            var cla1 = active.replace(reg1, 'a');
            $("." + cla1).show();
        }
        
        
    }

    //导航判断，折叠展开
    //$(".nav li.nav_list").click(function(){
    //    $(this).addClass("active").siblings().removeClass("active");
    //    $(this).find("i:first").addClass("fa-caret-down").parent().siblings().find("i").removeClass("fa-caret-down");
    //});
    $(".nav li.nav_list").click(function () {

        if ($(this).hasClass("active") === true) {
            //console.log(1233333)
            $(this).removeClass("active");
            $(this).find(".fa").removeClass("fa-caret-down");
        } else {
            $(this).addClass("active");
            $(this).find(".fa").addClass("fa-caret-down")
        }
    });

    $("li.nav_list .sub_nav li").click(function (event) {

        $(this).addClass("active").siblings().removeClass("active");
        // var tab_text = $(this).text();
        var tab_link = $(this).find("a").attr("href");
        if (tab_link != "javascript:void(0);" || tab_link != "javascript:;") {
        }
        event.preventDefault();
        event.stopPropagation();//阻止冒泡事件
    });

    //标签方式显示
    $('.nav li a').click(function () {
        var link = $(this).data("id"), name = $(this).text();
        if (!!link) {
            var i;

            if (link.getNav(true)) {
                i = $('.tabs ul').find('[data-id="' + link + '"]');
                var reg = new RegExp("/", "g");
                var cla = link.replace(reg, 'a');
                $("." + cla).show().siblings().hide();
            } else {
                link.setNav(name);
                i = $('<li data-id="' + link + '">' + name + '<i class="fa fa-times"></i></li>');
                $('.tabs ul').append(i);
                //iframe修改

                var reg = new RegExp("/", "g");
                var cla = link.replace(reg, 'a');
                var hit = $(window).height() - 120;
                var a = $('<iframe class="' + cla + '" src="' + link + '"></iframe>')
                $("#index_frame").append(a)
                a.height(hit)
                a.siblings().hide()
            }

            window.sessionStorage.setItem('nav-active', link);

            i.addClass('active').siblings().removeClass('active');
           
      
        }

    });
    //点击标签后操作
    $(document).on("click", ".tabs ul li",function(){
    //$(".tabs ul li").click(function () {
        $(this).addClass("active").siblings().removeClass("active");
        var link = $(this).data("id");
        window.sessionStorage.setItem('nav-active', link);
        //iframe修改      
        var reg = new RegExp("/", "g");
        var cla = link.replace(reg, 'a');
        $("." + cla).show().siblings().hide();
    });
    //标签删除
    $(document).on("click", ".tabs ul li .fa", function (event) {
    //$(".tabs ul li .fa").click(function (event) {
        var $parent = $(this).parent().prev();
        var $this = $(this).parent();
        if ($this.hasClass('active')) {
            $parent.addClass('active').siblings().removeClass('active');
            
        }
        var link = $(this).parent("li").data("id");
        console.log(link)
        //iframe修改
        var reg = new RegExp("/", "g");
        var cla = link.replace(reg, 'a');
        $("." + cla).remove();
        var cla1 = $parent.data('id').replace(reg, 'a');
        $("." + cla1).show();
        event.preventDefault();
        event.stopPropagation();//阻止冒泡事件
               
        $this.data('id').removeNav();
        $this.remove();
       
    });
    $(document).on("click", ".main_tab", function (event) {
        if ($("#index_frame").find(".aHomeaIndex").length == 0) {
            var hit = $(window).height() - 120;
            var a = $('<iframe class="aHomeaIndex" src="/Home/Index" ></iframe>')
            $("#index_frame").append(a)
            a.height(hit).siblings().hide();
        } else {
            $(".aHomeaIndex").siblings().hide();
        }
    })
    //表格下分页，为了快捷，直接用js渲染DOM
    var p_html = '<div class="paginate">' +
        '<div class="l_cont">已选<span class="red_num">4</span>条数据</div>' +
        '<div class="c_cont">' +
        '<ul>' +
        '<li class="first"><i class="fa fa-backward"></i></li>' +
        '<li class="prev"><i class="fa fa-caret-left"></i> </li>' +
        '<li>页码 <input type="text" class="goto" value="1"> </li>' +
        '<li>5</li>' +
        '<li class="next"><i class="fa fa-caret-right"></i> </li>' +
        '<li class="last"><i class="fa fa-forward"></i> </li>' +
        '<li>单页显示' +
        '<select>' +
        '<option value="10">10</option>' +
        '<option value="20">20</option>' +
        '<option value="30">30</option>' +
        '</select>' +
        '</li>' +
        '</ul>' +
        '</div>' +
        '<div class="r_cont">共<span class="red_num">8</span>条数据 </div>' +
        '</div>';
        $(".tab_cont").append(p_html);


    //折叠开展左侧导航菜单
    $(".head .slip").click(function () {
        $(".menu").toggleClass("hidden");
        $(".main_cont").toggleClass("show_all");
    });
    //退出操作
    //$(".main_header .cont:last-child").click(function () {
    //    window.location.href = "login.html";
    //    window.localStorage.removeItem("isLogin");
    //    window.localStorage.removeItem("theme_name");
    //});

});
